<template>
  <header>
    <div class="logo">PXDragon</div>
    <div class="head-menu">
      <h5><img src="../assets/icon/file.png" alt="" />File</h5>
      <ul>
        <li @click="onNew"><img src="../assets/icon/new.png" alt="" />New</li>
        <li @click="onClear">
          <img src="../assets/icon/clear.png" alt="" />Clear
        </li>
        <li class="right">
          <img src="../assets/icon/import.png" alt="" />Import
          <ul>
            <li>
              <input
                type="file"
                accept=".jpeg,.jpg,.png"
                @change="uploadImage"
              />Image
            </li>
          </ul>
        </li>
        <li class="right">
          <img src="../assets/icon/export.png" alt="" />Export
          <ul>
            <li @click="exportFile('px2css')">CSS</li>
            <li @click="exportFile('px2scss')">SCSS</li>
            <li @click="exportFile('px2png')">PNG</li>
          </ul>
        </li>
      </ul>
    </div>
    <div class="menu-btn" @click="onCancel" :class="{ none: historyNum == 0 }">
      <img src="../assets/icon/z.png" alt="" srcset="" />
    </div>
    <div class="menu-btn" @click="onRedo" :class="{ none: redoNum == 0 }">
      <img src="../assets/icon/y.png" alt="" srcset="" />
    </div>
    <div class="menu-btn" style="margin-left: auto" @click="openAbout">
      <img src="../assets/icon/config.png" alt="" srcset="" />
    </div>
    <div class="menu-btn" @click="isFull = !isFull">
      <img src="../assets/icon/full.png" alt="" srcset="" />
    </div>
  </header>
</template>

<script>
export default {
  data() {
    return {
      historyNum: 0,
      redoNum: 0,
      isFull: false,
    };
  },
  created() {
    this.onInfo();
  },
  watch: {
    isFull(v) {
      if (v) this.fullScreen();
      else this.exitFullscreen();
    },
  },
  methods: {
    onInfo() {
      this.$bus.$on("canvasInfo", (info) => {
        this.historyNum = info.historyList.length;
        this.redoNum = info.redoList.length;
      });
    },
    exportFile(type) {
      this.$bus.$emit("export", type);
    },
    onNew() {
      this.$bus.$emit("openNewDraw");
    },
    onClear() {
      this.$bus.$emit("clear");
    },
    onCancel() {
      this.$bus.$emit("cancel");
    },
    onRedo() {
      this.$bus.$emit("redo");
    },
    uploadImage(e) {
      let _this = this;
      let file = e.target.files[0];
      if (!file) return;
      let fileReader = new FileReader();
      fileReader.readAsDataURL(file);
      fileReader.onload = () => {
        _this.createImage(fileReader);
        e.target.value = "";
      };
    },
    createImage(obj) {
      let img = new Image();
      img.onload = () => {
        this.$bus.$emit("drawImage", img);
      };
      img.src = obj.result;
    },
    exitFullscreen() {
      if (document.exitFullscreen) {
        document.exitFullscreen();
      } else if (document.msExitFullscreen) {
        document.msExitFullscreen();
      } else if (document.mozCancelFullScreen) {
        document.mozCancelFullScreen();
      } else if (document.webkitExitFullscreen) {
        document.webkitExitFullscreen();
      }
    },
    fullScreen() {
      var element = document.documentElement;
      if (element.requestFullscreen) {
        element.requestFullscreen();
      } else if (element.msRequestFullscreen) {
        element.msRequestFullscreen();
      } else if (element.mozRequestFullScreen) {
        element.mozRequestFullScreen();
      } else if (element.webkitRequestFullscreen) {
        element.webkitRequestFullscreen();
      }
    },
    openAbout(){
      this.$bus.$emit("openAbout");
    }
  },
};
</script>

<style scoped lang="scss">
.menu-btn {
  width: 45px;
  height: 45px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  opacity: 1;
  &.none {
    opacity: 0.5;
    pointer-events: none;
  }
  &:hover {
    background-color: rgba(255, 255, 255, 0.2);
  }
  img {
    width: 50%;
    height: auto;
  }
}
.logo {
  width: 132px;
  height: 45px;
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 14px;
  color: #fff;
  font-family: "Oswald";

  &::before {
    content: "";
    width: 45px;
    height: 45px;
    background: url("../assets/logo.png") center center no-repeat;
    background-size: auto 100%;
  }
}
.head-menu {
  position: relative;
  width: 90px;
  height: 100%;
  font-family: "Oswald";
  .right::after {
    content: "";
    background: url("../assets/icon/right.png") no-repeat;
    background-size: 100% auto;
    width: 16px;
    height: 16px;
    margin-left: auto;
    margin-right: 10px;
  }
  input[type="file"] {
    position: absolute;
    left: 0;
    top: 0;
    bottom: 0;
    right: 0;
    z-index: 9;
    opacity: 0;
  }
  &:hover {
    & > ul {
      display: block;
    }
  }
  & > h5 {
    color: #fff;
    font-size: 14px;
    font-weight: normal;
    cursor: pointer;
    width: 100%;
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    &:hover {
      background-color: rgba(255, 255, 255, 0.2);
    }
    & > img {
      width: 18px;
      margin-right: 7px;
    }
  }
  & > ul {
    display: none;
    position: absolute;
    top: 45px;
    width: 150px;
    z-index: 9;
    box-sizing: border-box;
    box-shadow: -1px -1px 10px 0 rgb(48, 45, 45);
    background-color: #753a88;
    & > li {
      color: #fff;
      cursor: pointer;
      height: 45px;
      display: flex;
      align-items: center;
      box-sizing: border-box;
      padding: 0 0 0 23px;
      font-size: 14px;
      position: relative;
      & > img {
        width: 18px;
        margin-right: 7px;
      }
      &:hover {
        background-color: rgba(255, 255, 255, 0.2);
        & > ul {
          display: block;
        }
      }
      & > ul {
        display: none;
        position: absolute;
        left: 150px;
        width: 100px;
        background-color: #753a88;
        box-shadow: -1px -1px 10px 0 rgb(48, 45, 45);
        & > li {
          height: 36px;
          width: 100%;
          box-sizing: border-box;
          padding: 0 10px;
          display: flex;
          align-items: center;
          justify-content: center;
          position: relative;
          font-size: 14px;
          overflow: hidden;
          &:hover {
            background-color: rgba(255, 255, 255, 0.2);
          }
        }
      }
    }
  }
}
header {
  display: flex;
  width: 100%;
  height: 45px;
  background-color: #333;
  align-items: center;
  position: relative;
  z-index: 199;
  background-image: -webkit-gradient(
    linear,
    70% top,
    30% top,
    from(#cc2b5e),
    to(#753a88)
  );
}
</style>
